package com.ding.custommanagersys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ding.custommanagersys.bo.AreaBo;
import com.ding.custommanagersys.entity.Area;
import com.ding.custommanagersys.entity.ResultData;
import com.ding.custommanagersys.service.IAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ding
 * @since 2023-04-18
 */
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private IAreaService areaService;

    /**
     * 分页查询所有地区信息
    */
    @RequestMapping("/getAllArea")
    public ResultData<List<Area>> getAllArea(AreaBo bo){
        ResultData<List<Area>> resp = new ResultData<>();
        Page<Area> page = areaService.listAllArea(bo);
        resp.setData(page.getRecords());
        resp.setCount(page.getTotal());
        return resp.ok();
    }

    /**
     * 新增 地区信息
     */
    @PostMapping("/addArea")
    public ResultData<Integer> addArea(@RequestBody Area area){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = areaService.addArea(area);
        if(num == 0){
            resp.error("添加失败，请联系管理员！");
        }else{
            resp.ok();
        }
        return resp;
    }

    /**
     * 删除 地区信息
     */
    @PostMapping("/deleteArea")
    public ResultData<Integer> addArea(@RequestBody AreaBo bo){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = areaService.deleteArea(bo.getIds());
        if(num != 0){
            resp.ok();
        }else{
            resp.error("该编号下没有地区信息，或编号为空");
        }
        return resp;
    }

    /**
     * 修改 地区信息
     */
    @PostMapping("/updateArea")
    public ResultData<Integer> updateArea(Area area){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = areaService.updateArea(area);
        if(num == 0){
            resp.error("修改失败，请联系管理员！");
        }else{
            resp.ok();
        }
        return resp;
    }

}
